package com.example.weibo_suhuanzhen.retrofit;

import android.content.Context;
import android.util.Log;

import com.example.weibo_suhuanzhen.bean.BaseBean;
import com.example.weibo_suhuanzhen.homeRecycle.LikeCallback;
import com.example.weibo_suhuanzhen.sharedPreferences.SharedPreferencesManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LikeManager {
    final String TAG="shz_LikeManager";
    private Context context;
    private SharedPreferencesManager preferences;

    public LikeManager(Context context) {
        this.context = context;
        this.preferences = SharedPreferencesManager.getInstance(context.getApplicationContext());
    }

    public void sendLike(Long id , LikeCallback callback){
        ShzApi shzApi = RetrofitManager.getInstance().createApi();
        ShzApi.LikeEventRequest likeEventRequest = new ShzApi.LikeEventRequest(id);
        Call<BaseBean<Boolean>> call = shzApi.sendLike("Bearer " + preferences.getData("token",""), likeEventRequest);
        call.enqueue(new Callback<BaseBean<Boolean>>() {
            @Override
            public void onResponse(Call<BaseBean<Boolean>> call, Response<BaseBean<Boolean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<Boolean> body = response.body();
                    if (body != null && body.data!=null && body.data && body.code==200) {
                        Log.d(TAG, "点赞成功");
                        callback.onSuccess();
                    } else {
                        Log.d(TAG, "点赞失败：" + body != null ? body.msg : "unknown error");
                        callback.onFailure();
                    }
                } else {
                    Log.d(TAG, "响应失败：" + response.message());
                    callback.onFailure();
                }
            }
            @Override
            public void onFailure(Call<BaseBean<Boolean>> call, Throwable t) {
                Log.d(TAG, "请求失败：" + t.getMessage());
                callback.onFailure();
            }
        });
    }

    public void sendCancelLike(Long id, LikeCallback callback){
        ShzApi shzApi = RetrofitManager.getInstance().createApi();
        ShzApi.LikeEventRequest likeEventRequest = new ShzApi.LikeEventRequest(id);
        Call<BaseBean<Boolean>> call = shzApi.sendCancelLike("Bearer " + preferences.getData("token",""), likeEventRequest);
        call.enqueue(new Callback<BaseBean<Boolean>>() {
            @Override
            public void onResponse(Call<BaseBean<Boolean>> call, Response<BaseBean<Boolean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<Boolean> body = response.body();
                    if (body != null && body.data!=null && body.data && body.code==200) {
                        Log.d(TAG, "取消点赞成功");
                        callback.onSuccess();
                    } else {
                        Log.d(TAG, "取消点赞失败：" + body != null ? body.msg : "unknown error");
                        callback.onFailure();
                    }
                } else {
                    Log.d(TAG, "响应失败：" + response.message());
                    callback.onFailure();
                }
            }
            @Override
            public void onFailure(Call<BaseBean<Boolean>> call, Throwable t) {
                Log.d(TAG, "请求失败：" + t.getMessage());
                callback.onFailure();
            }
        });
    }
}
